package org.ben.birthday.twentybirthday.service.impl;

import org.ben.birthday.twentybirthday.dao.BackGroundMovieDaoRepository;
import org.ben.birthday.twentybirthday.entity.BackGroundMovie;
import org.ben.birthday.twentybirthday.service.IBackGroundMovieService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * @Author: Ben
 * @Date: 2019/8/8 18:41
 */
@Service
public class BackGroundMovieImpl implements IBackGroundMovieService {

    @Autowired
    BackGroundMovieDaoRepository backGroundMovieDaoRepository;

    private static final Logger logger = LoggerFactory.getLogger(BackGroundMovieImpl.class);

    /**
     * 增加视频Service
     * @param backGroundMovie
     * @return boolean
     */
    @Override
    public boolean insertBackGroundMovieService(BackGroundMovie backGroundMovie) {
        List<BackGroundMovie> backGroundMovieList=backGroundMovieDaoRepository.findBackGroundMovieByMovieUrl(backGroundMovie.getMovieUrl());
        Iterator<BackGroundMovie> backGroundMovieIterator=backGroundMovieList.iterator();
        while (backGroundMovieIterator.hasNext()){
            logger.error("该视频已存在，添加失败");
            return false;
        }
        backGroundMovieDaoRepository.save(backGroundMovie);
        logger.info("视频添加成功");
        return true;
    }

    /**
     * 删除视频Service
     * @param backGroundMovieId
     * @param movieUrl
     * @return boolean
     */
    @Override
    public boolean deleteBackGroundMovieService(Integer backGroundMovieId, String movieUrl) {
       List<BackGroundMovie> backGroundMovieList=backGroundMovieDaoRepository.findBackGroundMovieByMovieUrl(movieUrl);
       Iterator<BackGroundMovie> backGroundMovieIterator=backGroundMovieList.iterator();
       while(backGroundMovieIterator.hasNext()){
           backGroundMovieDaoRepository.deleteById(backGroundMovieId);
           logger.info("视频删除成功");
           return true;
       }
       logger.error("视频不存在，删除失败");
        return false;
    }

    /**
     *更改视频Service
     * @param backGroundMovie
     * @return boolean
     */
    @Override
    public boolean updateBackGroundMovieService(BackGroundMovie backGroundMovie) {
        List<BackGroundMovie> backGroundMovieList=backGroundMovieDaoRepository.findBackGroundMovieByMovieUrl(backGroundMovie.getMovieUrl());
        Iterator<BackGroundMovie> backGroundMovieIterator=backGroundMovieList.iterator();
        while (backGroundMovieIterator.hasNext()){
            backGroundMovieDaoRepository.saveAndFlush(backGroundMovie);
            logger.info("背景视频更改成功");
            return true;
        }
        logger.error("查无此背景视频，更改失败");
        return false;
    }


    /**
     * 根据backGroundMovieId查询背景视频信息Service
     * @param backGroundMovieId
     * @return BackGroundMovie
     */
    @Override
    public BackGroundMovie selectBackGroundMovieService(Integer backGroundMovieId) {
        Optional<BackGroundMovie> optionalBackGroundMovie=backGroundMovieDaoRepository.findById(backGroundMovieId);
        boolean judgment=optionalBackGroundMovie.isPresent();
        if (judgment){
         BackGroundMovie backGroundMovie=optionalBackGroundMovie.get();
         logger.info(backGroundMovie.toString());
         return backGroundMovie;
        }else {
            logger.error("您");
            return null;
        }

    }
}
